package leetcode.L500;

import java.util.Deque;
import java.util.LinkedList;

public class EX407_2 {
    public static void main(String[] args) {
        String num = "1234567890";
        int k = 9;


        Deque<Character> deque = new LinkedList<Character>();
        for (int i = 0; i < num.length(); i++) {
            char digit = num.charAt(i);
            while(!deque.isEmpty()&&k>0&&deque.peekLast()>digit){
                deque.pollLast();
                k--;
            }
            deque.addLast(digit);
        }
        for (int i = 0; i < k ; i++) {
            deque.pollLast();
        }

        StringBuilder ret = new StringBuilder();
        boolean leadingZero = false;
        while (!deque.isEmpty()){
            char digit = deque.pollFirst();
            if(digit != '0'||leadingZero){
                leadingZero = true;
                ret.append(digit);
            }
        }
        System.out.println(ret.length() == 0 ? "0":ret.toString());

    }
}
